#!/bin/bash

 # Menu

clear
user=`users`
echo "" Hi, $user 
echo ""
echo "		USB Install-Stick erstellen - Creating your Flash Drive"
echo ""
echo "		- 1 - Yosemite "
echo ""
echo "		- 2 - El Capitan "
echo ""
echo "		- 3 - Sierra Deveoper Preview "
echo ""
echo ""
echo "          Die Install.apps müssen im '/Programme'-Ordner liegen "
echo "          The Install.apps must be in the '/Applications'-Folder "
echo ""
echo "		🛠 "" Wähle eine Option - Choose an option 🛠 "
read response

case "$response" in

 # Mount the installer Yosemite
 1)echo "Du hast Yosemite ausgewählt, welches USB Gerät soll genutzt werden?"
echo ""
echo "USB Install-Stick per Drag and Drop hinzufügen"
echo ""
echo ""
echo "You chose Yosemite, On what media want to install?"
echo ""
echo "Drag and Drop your Flash Drive"
read support

hdiutil attach /Applications/Install\ OS\ X\ Yosemite.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app

# Creation of a temporary image of 7316 mb
 hdiutil create -o /tmp/Yosemite -size 7316m -layout SPUD -fs HFS+J -type SPARSE
 
 # Mount the temporary image
 hdiutil attach /tmp/Yosemite.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build
 
 # Restore Base System in the temporary image
 asr restore -source /Volumes/install_app/BaseSystem.dmg -target /Volumes/install_build -noprompt -noverify -erase
 
 # copy installation files
 rm /Volumes/OS\ X\ Base\ System/System/Installation/Packages
 cp -rp /Volumes/install_app/Packages /Volumes/OS\ X\ Base\ System/System/Installation/
 
 # copy installation dependencies
 cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/OS\ X\ Base\ System/BaseSystem.chunklist
 cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/OS\ X\ Base\ System/BaseSystem.dmg

 # restore the System image to the USB drive
 sudo asr restore -source /Volumes/OS\ X\ Base\ System/ -target "$support" -erase

 # unmount the installer
 hdiutil detach /Volumes/install_app

 # Unmount the Base System image
 hdiutil detach /Volumes/OS\ X\ Base\ System/

 # delete Temporary image
  rm -f /tmp/Yosemite.sparseimage

# rename the USB drive
diskutil rename /Volumes/OS\ X\ Base\ System\ 1 Yosemite\ Installation
;;

 # Mount the installer el Capitan
 2)echo "Du hast El Capitan ausgewählt, welches USB Gerät soll genutzt werden?"
echo ""
echo "USB Install-Stick per Drag and Drop hinzufügen"
echo ""
echo ""
echo "You chose El Capitan, On what media want to install?"
echo ""
echo "Drag and Drop your Flash Drive"
read support
hdiutil attach /Applications/Install\ OS\ X\ El\ Capitan.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app

 # Creation of a temporary image of 7316 mb
 hdiutil create -o /tmp/ElCapitan -size 7316m -layout SPUD -fs HFS+J -type SPARSE
 
 # Mount the temporary image
 hdiutil attach /tmp/ElCapitan.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build
 
 # Restore Base System in the temporary image
 asr restore -source /Volumes/install_app/BaseSystem.dmg -target /Volumes/install_build -noprompt -noverify -erase
 
 # copy installation files
 rm /Volumes/OS\ X\ Base\ System/System/Installation/Packages
 cp -rp /Volumes/install_app/Packages /Volumes/OS\ X\ Base\ System/System/Installation/
 
 # copy installation dependencies
 cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/OS\ X\ Base\ System/BaseSystem.chunklist
 cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/OS\ X\ Base\ System/BaseSystem.dmg

 # restore the System image to the USB drive
 sudo asr restore -source /Volumes/OS\ X\ Base\ System/ -target "$support" -erase

 # unmount the installer
 hdiutil detach /Volumes/install_app

 # Unmount the Base System image
 hdiutil detach /Volumes/OS\ X\ Base\ System/

 # delete Temporary image
  rm -f /tmp/ElCapitan.sparseimage

# rename the USB drive
diskutil rename /Volumes/OS\ X\ Base\ System\ 1 El\ Capitan\ Installation

;;

 # Mount the installer Sierra Dev Preview
 3)echo "Du hast Sierra Developer Preview ausgewählt, welches USB Gerät soll genutzt werden?"
echo ""
echo "USB Install-Stick per Drag and Drop hinzufügen"
echo ""
echo ""
echo "You chose Sierra Developer Preview, On what media want to install?"
echo ""
echo "Drag and Drop your Flash Drive"
read support
hdiutil attach /Applications/Install\ 10.12\ Developer\ Preview.app/Contents/SharedSupport/InstallESD.dmg -noverify -nobrowse -mountpoint /Volumes/install_app

 # Creation of a temporary image of 7316 mb
 hdiutil create -o /tmp/Sierra -size 7316m -layout SPUD -fs HFS+J -type SPARSE
 
 # Mount the temporary image
 hdiutil attach /tmp/Sierra.sparseimage -noverify -nobrowse -mountpoint /Volumes/install_build
 
 # Restore Base System in the temporary image
 asr restore -source /Volumes/install_app/BaseSystem.dmg -target /Volumes/install_build -noprompt -noverify -erase
 
 # copy installation files
 rm /Volumes/OS\ X\ Base\ System/System/Installation/Packages
 cp -rp /Volumes/install_app/Packages /Volumes/OS\ X\ Base\ System/System/Installation/
 
 # copy installation dependencies
 cp -rp /Volumes/install_app/BaseSystem.chunklist /Volumes/OS\ X\ Base\ System/BaseSystem.chunklist
 cp -rp /Volumes/install_app/BaseSystem.dmg /Volumes/OS\ X\ Base\ System/BaseSystem.dmg

 # restore the System image to the USB drive
 sudo asr restore -source /Volumes/OS\ X\ Base\ System/ -target "$support" -erase

 # unmount the installer
 hdiutil detach /Volumes/install_app

 # Unmount the Base System image
 hdiutil detach /Volumes/OS\ X\ Base\ System/

 # delete Temporary image
  rm -f /tmp/Sierra.sparseimage

# rename the USB drive
diskutil rename /Volumes/OS\ X\ Base\ System\ 1 Sierra\ Developer\ Preview\ Installation

;;
esac
